<?php

class Home extends Controller {
	
	function __construct() {
		parent::Controller();
		
		if ($this->session->userdata('logged_in') != TRUE) {
			redirect('users/login');
		}
		
		$this->load->model('home_model');
	}
	
	function index() {
		redirect('home/main');
	}
	
	function main($message = NULL) {
		$user_id = $this->session->userdata('user_id');
		$user_level = $this->home_model->get_user_level($user_id);
		$started_by_user = $this->home_model->get_processes_started_by_user($user_id);
		
		if (($user_level == PROCESS_MANAGER) || ($user_level == ADMIN)) {
			$processes = $this->home_model->get_all_processes();
		}
		else {
			$processes = $this->home_model->get_processes_owned_by_user($user_id);
		}
		
		$participating_processes = $this->home_model->get_processes_where_user_participates_in($user_id);
		
		$this->load->view('template/standard', array(
			'title'			=> 'Homepage',
			'message'		=> $message,
			'content'		=> $this->load->view('home/main', array(
				'user_level'				=> $user_level,
				'started_by_user'			=> $started_by_user,
				'participating_processes'	=> $participating_processes,
				'processes'					=> $processes
			), TRUE),
			'menu_items'	=> array(
				site_url('home/finished_processes') => 'Recently finished processes'
			)
		));
	}
	
	function finished_processes() {
		$processes = $this->home_model->get_latest_processes($this->session->userdata('user_id'));
		
		$this->load->view('template/standard', array(
			'title'		=> 'Recently finished processes',
			'content'	=> $this->load->view('home/finished_processes', array(
				'processes'	=> $processes
			), TRUE)
		));
	}
	
	function process_details($process_instance_id) {
		$data = $this->home_model->get_process_details($process_instance_id);
		
		$this->load->view('template/standard', array(
			'title'		=> 'Process details',
			'content'	=> $this->load->view('home/process_details', $data, TRUE)
		));
	}
	
	function download_file($document_id) {
		$this->load->helper('download');
		
		$data = $this->home_model->get_file($document_id);
		
		if (count($data) != 0) {
			force_download($data['filename'], $data['filecontent']);
		}
		else {
			show_404();
		}
	}
	
	function start_process() {
		if (func_num_args() != 0) {
			$args = func_get_args();
			$process_id = $args[0];
		}
		else {
			$process_id = $this->input->post('process');
		}

		$done = $this->home_model->start_process($this->session->userdata('user_id'), $process_id);
		$message = $done ? 'Process started' : 'Process is not started, because the system didn\'t find a first stage. Please check this process and make sure it has 1 stage without a previous stage defined.';
		
		$this->main($message);
	}
	
	function participate($stage_instance_id) {
		
		// Check if the user participates in this stage
	
		$user_participates = $this->home_model->get_user_participation($this->session->userdata('user_id'), $stage_instance_id);
		
		if (!$user_participates) {
			redirect('home/main');
		}
		
		// Get stage details
		
		$stage_details = $this->home_model->get_stage_details($stage_instance_id);
		
		// Get contribution details
		
		$contribution_details = $this->home_model->get_all_contributions($stage_instance_id, $stage_details['stage_id']);
		
		// Check if finish button can be activated
		
		$finish_button_activated = $this->home_model->is_all_contributions_finished($stage_instance_id);
		
		// Print out everything
		
		$data = array(
			'stage_name'		=> $stage_details['stage_name'],
			'stage_description'	=> $stage_details['stage_description'],
			'contributions'		=> $contribution_details,
			'stage_instance_id'	=> $stage_instance_id,
			'finish_button_activated'	=> $finish_button_activated
		);
		
		$this->load->view('template/standard', array(
			'title'		=> 'Participate',
			'content'	=> $this->load->view('home/participate_list', $data, TRUE)
		));
	}
	
	function participate_form($stage_instance_id, $contribution_id) {
		
		$message = NULL;
	
		// Check if the user participates in this stage
		
		$user_participates = $this->home_model->get_user_participation($this->session->userdata('user_id'), $stage_instance_id);
		
		if (!$user_participates) {
			redirect('home/main');
		}
		
		// Get contribution and attributes details
		
		$contribution_details = $this->home_model->get_contribution_details($contribution_id);
		
		$attributes = $this->home_model->get_attributes($contribution_id);
		
		$files = array();
		$checkboxes = array();
		foreach ($attributes as $att) {
			if ($att['attribute_type'] == 'FILE') {
				$files[] = $att['attribute_id'];
			}
			elseif ($att['attribute_type'] == 'CHECKBOX') {
				$checkboxes[] = $att['attribute_id'];
			}
		}
		
		// Process posted data
		if (isset($_POST['att'])) {
			$post_attributes = $this->input->post('att');
			
			$not_ok = false;
			
			foreach ($post_attributes as $post_att_id => $post_att) {
				if (strlen($post_att) == 0) {
					foreach ($attributes as $att) {
						if ($att['attribute_id'] == $post_att_id) {
							$not_ok = $not_ok || ($att['optional'] == '0');
						}
					}
				}
			}
			
			foreach ($files as $file) {
				if (!isset($_FILES['att'])) {
					$not_ok = true;
				}
			}
			
			if (!$not_ok) {
				if (count($checkboxes) != 0) {
					foreach ($checkboxes as $box) {
						$post_attributes[$box] = (isset($post_attributes[$box]) ? 'true' : 'false');
					}
				}
			
				$contribution_instance_id = $this->home_model->create_contribution_instance($contribution_id, $this->session->userdata('user_id'), $stage_instance_id);
				
				$this->home_model->set_attributes($contribution_instance_id, $post_attributes);
				
				if (count($files) != 0) {
					
					foreach ($files as $file) {
						$file_content = file_get_contents(current($_FILES['att']['tmp_name']));
						$file_name = current($_FILES['att']['name']) . '';
						$file_size = current($_FILES['att']['size']) . '';
						
						$this->home_model->set_file($contribution_instance_id, $file, $file_name, $file_size, $file_content);
					}
				}
				
				redirect('home/participate/' . $stage_instance_id);
			}
			else {
				$message = 'Please fill in all obliged fields.';
			}
		}
		
		
		// Print out everything
		
		$data = array(
			'message'					=> $message,
		
			'stage_instance_id'			=> $stage_instance_id,
		
			'attributes'				=> $attributes,
		
			'contribution_title'		=> $contribution_details['contribution_title'],
			'contribution_description'	=> $contribution_details['contribution_description']
		);
		
		$this->load->view('template/standard', array(
			'title'		=> 'Participate',
			'content'	=> $this->load->view('home/participate_form', $data, TRUE)
		));
	}
	
	function finish($stage_instance_id) {
		
		if ($this->home_model->is_all_contributions_finished($stage_instance_id)) {
			$this->home_model->finish_stage($stage_instance_id, $this->session->userdata('user_id'));
		}
		
		redirect('home');
	}
}

?>